#include <iostream>
#include"head.h"
void Release(Node* phead) //释放链表
{    //释放链表
   Node* p  =  phead;
    while (p !=  NULL)
    {
        phead  =  phead->next;

        free(p);

        p  =  phead;
    }
	return;
}

void Traversal(Node* phead, void pFunc2(void* data))//遍历链表
{  //遍历链表
	Node* p = phead->next;
	while(p != NULL)
	{
		pFunc2(p->data);//调用函数，显示结点p数据域 
		
		p = p->next;
	}
	return;
}

Node *AddAll(Node* phead,   int n,   void* (*pFunc)())//增加链表结点
{
	for(int i  =  0; i < n; i++)
	{
		Node *pnew  =  (Node*)malloc(sizeof(Node));

		pnew->data = pFunc();/*通过函数指针对数据域赋值*/
		pnew->next  =  NULL;
		
		pnew->next = phead->next;
		phead->next = pnew;

	}
	return phead;
}  

void find_Employee(void*data, char str[20])
{
	Employee* E = (Employee*)data; 
	
	if(strcmp(E->name,str) == 0)
	{
		ShowEmployee(data);
	}
}

void insertion_Employee(Node* phead)
{
	void* p= InputEmployee();
	//确认是否修改
	while(1)
	{
	    printf("是否插入？\nY or N\n");
		char choose[0];
		scanf("%s",choose);

		if(strcmp(choose,"Y")==0){
			Node *pnew  =  (Node*)malloc(sizeof(Node));
			pnew->next = NULL;
			pnew->data = p;

			pnew->next = phead->next;
			phead->next = pnew;

			return;
		}
		if(strcmp(choose,"N")==0){
			free(p);
			return;
		}
		printf("选择错误，重新选择Y or N！！！\n");
	}
}

void delete_Employee(Node* phead,char str[20])
{
	while(1)
	{

		printf("是否进行删除操作？\nY or N\n");
		char choose[0];
		scanf("%s",choose);

		if(strcmp(choose,"N") == 0){
			return;
		}
		if(strcmp(choose,"Y") == 0){

			Node* p = phead;
			while(p->next!= NULL)
			{
				Employee*E = (Employee*)malloc(sizeof(Employee));
				E=(Employee*)p->next->data;

				if (strcmp(E->name,str) == 0)
				{
					p->next=p->next->next;
					return;
				}

				p = p->next;
			}
			if(phead->data == NULL){
			printf("未找到姓名为%s的职工", str);
			return;
			}
			if(p->data == NULL){
				printf("未找到姓名为%s的职工", str);
				return;
			}
		}
		printf("选择错误，重新选择Y or N！！！\n");
	}

	return;
}

void change_Employee(Node* phead,char str[20])
{
	while(1)
	{

		printf("是否进行修改操作？\nY or N\n");
		char choose[0];
		scanf("%s",choose);

		if(strcmp(choose,"N") == 0){
			return;
		}
		if(strcmp(choose,"Y") == 0)
		{
			Node* p = phead->next;
			while(p!= NULL)
			{
				Employee*E = (Employee*)malloc(sizeof(Employee));
				E=(Employee*)p->data;

				if (strcmp(E->name,str) == 0)
				{
					p->data=InputEmployee();
					return;
				}

				p = p->next;
			}
			if (phead->data == NULL)
			{
				printf("未找到姓名为%s的职工", str);
				return;
			}
			if (p->data == NULL)
			{
				printf("未找到姓名为%s的职工", str);
				return;
			}
		}
		printf("选择错误，重新选择Y or N！！！\n");
	}

	return;
}
